package com.hxh.util;

import cn.hutool.core.util.StrUtil;
import com.hxh.entity.Project;
import com.hxh.entity.ProjectDetail;
import com.hxh.entity.po.ClassInfo;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.charset.StandardCharsets;

import static com.hxh.config.Constants.ProjectDirSuffix;

/**
 *
 */
public class GenUtil {
    //创建时间 - mapper 自动处理
    public static final String AutoProcessCreateTimeFields = "create_time";
    //修改时间 - mapper 自动处理
    public static final String AutoProcessUpdateTimeFields = "update_time";
    //创建者 - service update时自动处理
    public static final String AutoProcessUpdaterFields = "updater";
    //新增人 - service insert时自动处理
    public static final String AutoProcessCreatorFields = "creator";

    public static File newFile(Project project, String filePath, String filename) throws IOException {
        ProjectDetail backendProject = project.getBackendProject();
        String pathPre = backendProject.getProjectDir() + backendProject.getProjectName() + File.separator + ProjectDirSuffix + File.separator
                + backendProject.getProjectGroup().replaceAll("\\.", "\\\\\\\\");
        String resourcesPre = ProjectDirSuffix.substring(0, ProjectDirSuffix.lastIndexOf(File.separator));
        String path;
        if (StringUtils.isBlank(filePath)) {
            path = pathPre + File.separator + filename + ".java";
        } else {
            switch (filePath) {
                case "pom":
                    path = backendProject.getProjectDir() + backendProject.getProjectName() + File.separator + "pom.xml";
                    break;
                case "applicationConfig":
                    path = backendProject.getProjectDir() + backendProject.getProjectName() + File.separator + resourcesPre + "\\resources\\config\\" + filename + ".yml";
                    break;
                case "mapper":
                    path = backendProject.getProjectDir() + backendProject.getProjectName() + File.separator + resourcesPre + "\\resources\\mapper\\" + filename + ".xml";
                    break;
                case "menu":
                    path = backendProject.getProjectDir() + backendProject.getProjectName() + File.separator + resourcesPre + "\\resources\\" + filename + ".xml";
                    break;
                case "application":
                    path = pathPre + "\\Application.java";
                    break;
                default:
                    path = pathPre + File.separator + filePath + File.separator + filename + ".java";

            }
        }

        return newFileWithBak(path);
    }

    public static File newFileWithBak(String path) throws IOException {
        File file = new File(path);
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
        if (!file.exists()) {
            file.createNewFile();
        } else {
            String oriPath = file.getAbsolutePath();
            File bakFile = new File(oriPath + ".bak");
            if (bakFile.exists()) {
                bakFile.delete();
            }
            file.renameTo(bakFile);
        }
        return file;
    }

    /**
     * @param project:  项目
     * @param type:     类型
     * @param filename: 文件名 为空则只创建文件夹
     * @return java.io.File
     * @author HXH
     * @description:
     * @date 2023/12/4
     **/
    public static File newVueFile(Project project, String type, String filename) throws IOException {
        ProjectDetail frontendProject = project.getFrontendProject();
        File file;
        String pathPre = frontendProject.getProjectDir() + frontendProject.getProjectName();
        String path;

        path = pathPre + File.separator + type + File.separator;
        if (StringUtils.isNotBlank(filename)) {
            path += filename;
        }
        file = new File(path);

        if (StringUtils.isNotBlank(filename)) {
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            if (!file.exists()) {
                file.createNewFile();
            } else {
                String oriPath = file.getAbsolutePath();
                File bakFile = new File(oriPath + ".bak");
                if (bakFile.exists()) {
                    bakFile.delete();
                }
                file.renameTo(bakFile);
            }
        } else {
            if (!file.exists()) {
                file.mkdirs();
            }
        }
        return file;
    }

    public static void clearAllBakFile(String path) {
        File file = new File(path);
        String name = file.getName();
        if ("node_modules".equals(name) || ".idea".equals(name) || "target".equals(name)) {
            //跳过某些目录
            return;
        }
        if (!file.exists() || file.isFile()) {
            return;
        }
        File[] files = file.listFiles();
        for (File f : files) {
            if (f.isDirectory()) {
                clearAllBakFile(f.getAbsolutePath());
            } else {
                if (f.getName().endsWith(".bak")) {
                    f.delete();
                }
            }
        }
    }

    public static String getMutilDatasourceStr(Project project, ClassInfo classInfo) {
        if (project == null) {
            return "";
        }
        String mutilDatasourceId = project.getMutilDatasourceId();
        if (StrUtil.isBlank(mutilDatasourceId)) {
            return "";
        }
        String mysqlDatabaseName = classInfo.getProjectDatasource().getMysqlDatabaseName();
        return StrUtil.toCamelCase(mysqlDatabaseName);
    }

    public static String getMutilDatasourceStrByConfig(Project project, ClassInfo classInfo, String prefix, String suffix) {
        String datasourceStr = getMutilDatasourceStr(project, classInfo);
        if (StrUtil.isBlank(datasourceStr)) {
            return "";
        }
        if (StrUtil.isNotBlank(prefix)) {
            datasourceStr = prefix + datasourceStr;
        }
        if (StrUtil.isNotBlank(suffix)) {
            datasourceStr = datasourceStr + suffix;
        }
        return datasourceStr;
    }
}
